package centers

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
    "github.com/flipped-aurora/gin-vue-admin/server/model/centers"
    "github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
    centersReq "github.com/flipped-aurora/gin-vue-admin/server/model/centers/request"
    "github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
    "github.com/flipped-aurora/gin-vue-admin/server/service"
    "github.com/gin-gonic/gin"
    "go.uber.org/zap"
)

type CentersApi struct {
}

var centerService = service.ServiceGroupApp.CentersServiceGroup.CentersService


// CreateCenters 创建Centers
// @Tags Centers
// @Summary 创建Centers
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body centers.Centers true "创建Centers"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /center/createCenters [post]
func (centerApi *CentersApi) CreateCenters(c *gin.Context) {
	var center centers.Centers
	err := c.ShouldBindJSON(&center)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := centerService.CreateCenters(&center); err != nil {
        global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteCenters 删除Centers
// @Tags Centers
// @Summary 删除Centers
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body centers.Centers true "删除Centers"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /center/deleteCenters [delete]
func (centerApi *CentersApi) DeleteCenters(c *gin.Context) {
	var center centers.Centers
	err := c.ShouldBindJSON(&center)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := centerService.DeleteCenters(center); err != nil {
        global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteCentersByIds 批量删除Centers
// @Tags Centers
// @Summary 批量删除Centers
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除Centers"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /center/deleteCentersByIds [delete]
func (centerApi *CentersApi) DeleteCentersByIds(c *gin.Context) {
	var IDS request.IdsReq
    err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := centerService.DeleteCentersByIds(IDS); err != nil {
        global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateCenters 更新Centers
// @Tags Centers
// @Summary 更新Centers
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body centers.Centers true "更新Centers"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /center/updateCenters [put]
func (centerApi *CentersApi) UpdateCenters(c *gin.Context) {
	var center centers.Centers
	err := c.ShouldBindJSON(&center)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := centerService.UpdateCenters(center); err != nil {
        global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindCenters 用id查询Centers
// @Tags Centers
// @Summary 用id查询Centers
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query centers.Centers true "用id查询Centers"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /center/findCenters [get]
func (centerApi *CentersApi) FindCenters(c *gin.Context) {
	var center centers.Centers
	err := c.ShouldBindQuery(&center)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if recenter, err := centerService.GetCenters(center.ID); err != nil {
        global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"recenter": recenter}, c)
	}
}

// GetCentersList 分页获取Centers列表
// @Tags Centers
// @Summary 分页获取Centers列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query centersReq.CentersSearch true "分页获取Centers列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /center/getCentersList [get]
func (centerApi *CentersApi) GetCentersList(c *gin.Context) {
	var pageInfo centersReq.CentersSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := centerService.GetCentersInfoList(pageInfo); err != nil {
	    global.GVA_LOG.Error("获取失败!", zap.Error(err))
        response.FailWithMessage("获取失败", c)
    } else {
        response.OkWithDetailed(response.PageResult{
            List:     list,
            Total:    total,
            Page:     pageInfo.Page,
            PageSize: pageInfo.PageSize,
        }, "获取成功", c)
    }
}
